
var allpoint=null;
var dpGuid = null;
$(function () {
    $.when(
        $.get(baseURL + 'csg/cschedulereport/select?pGuid='+ GetUrlParam("pGuid")+'&sGuid='+GetUrlParam("sGuid"), function (r) {
            allpoint = r.returnlist;
            vm.allpoint=allpoint;
            // console.log("allpoint="+JSON.stringify(vm.allpoint))
        })

    ).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'bss/dpicture/list?pStatus=施工&pLabel=进度,xc&sGuid='+ GetUrlParam("sGuid"),
            datatype: "json",
            colModel: [
                { label: '图片', name: 'pServeraddress', index: 'p_serveraddress', width: 80,
                    align: "center", sortable: false, editable: false, formatter: alarmFormatter
                },

                { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true  },
                { label: '所属进度记录', name: 'pPGuid', index: 'p_p_guid', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in allpoint) {
                            if (cellvalue == allpoint[d].guid) return allpoint[d].sRTheme;
                        }
                    }
                },
                { label: '照片来源', name: 'pPResour', index: 'p_p_resour', width: 80 },
                { label: '点位类型', name: 'pPType', index: 'p_p_type', width: 80, hidden: true },
                { label: '照片标签', name: 'pLabel', index: 'p_label', width: 80, hidden: true },
                { label: '拍照时间', name: 'pTime', index: 'p_time', width: 80,
                    formatter:function(value,options,row){
                        return value!=null?new Date(value).Format('yyyy-MM-dd'):"";}
                },
                { label: '照片名称', name: 'pName', index: 'p_name', width: 80, hidden: true },
                { label: '经度', name: 'pLongitude', index: 'p_longitude', width: 80, hidden: true },
                { label: '纬度', name: 'pLatitude', index: 'p_latitude', width: 80, hidden: true },
                { label: '项目状态', name: 'pStatus', index: 'p_status', width: 80, hidden: true }
            ],
            viewrecords: true,
            height: 385,
            rowNum: 10,
            rowList : [10,30,50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth:true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader : {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames : {
                page:"page",
                rows:"limit",
                order: "order"
            },
            gridComplete:function(){
                $('#jqGrid img').click(function (e) {
                    var _this = $(this);//将当前的pimg元素作为_this传入函数
                    imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
                });


                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        })
    )
});
function imgShow(outerdiv, innerdiv, bigimg, _this){
    var src = _this.attr("src");//获取当前点击的pimg元素中的src属性
    $(bigimg).attr("src", src);//设置#bigimg元素的src属性

    /*获取当前点击图片的真实大小，并显示弹出层及大图*/
    $("<img/>").attr("src", src).load(function(){
        var windowW = $(window).width();//获取当前窗口宽度
        var windowH = $(window).height();//获取当前窗口高度
        var realWidth = this.width;//获取图片真实宽度
        var realHeight = this.height;//获取图片真实高度
        var imgWidth, imgHeight;
        var scale = 0.8;//缩放尺寸，当图片真实宽度和高度大于窗口宽度和高度时进行缩放

        if(realHeight>windowH*scale) {//判断图片高度
            imgHeight = windowH*scale;//如大于窗口高度，图片高度进行缩放
            imgWidth = imgHeight/realHeight*realWidth;//等比例缩放宽度
            if(imgWidth>windowW*scale) {//如宽度扔大于窗口宽度
                imgWidth = windowW*scale;//再对宽度进行缩放
            }
        } else if(realWidth>windowW*scale) {//如图片高度合适，判断图片宽度
            imgWidth = windowW*scale;//如大于窗口宽度，图片宽度进行缩放
            imgHeight = imgWidth/realWidth*realHeight;//等比例缩放高度
        } else {//如果图片真实高度和宽度都符合要求，高宽不变
            imgWidth = realWidth;
            imgHeight = realHeight;
        }
        $(bigimg).css("width",imgWidth);//以最终的宽度对图片缩放

        var w = (windowW-imgWidth)/2;//计算图片与窗口左边距
        var h = (windowH-imgHeight)/2;//计算图片与窗口上边距
        $(innerdiv).css({"top":h, "left":w});//设置#innerdiv的top和left属性
        $(outerdiv).fadeIn("fast");//淡入显示#outerdiv及.pimg
    });

    $(outerdiv).click(function(){//再次点击淡出消失弹出层
        $(this).fadeOut("fast");
    });
}
function alarmFormatter(cellvalue, options, rowdata) {
    return ' <img  src="' +baseURL+'rest/photo/queryPic?address='+ cellvalue + '"  id="img' + rowdata.guid + '"  style="width:40px;height:40px;" />';
}
var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        allpoint:allpoint,
        dpGuid:dpGuid,
        dPicture: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.dPicture = {};
        },
        update: function (event) {
            var guid = getSelectedRow();
            if(guid == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.dPicture.guid == null ? "bss/dpicture/save" : "bss/dpicture/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.dPicture),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if(guids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "bss/dpicture/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(guid){
            $.get(baseURL + "bss/dpicture/info/"+guid, function(r){
                vm.dPicture = r.dPicture;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});